﻿using System;
using System.Windows.Input;
using System.Windows;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace ThreeDWorld
{
    public static class Input
    {
        private static Dictionary<Key, bool> _KeyState;

        public static bool LeftMouseDown
        {
            get;
            set;
        }

        public static bool RightMouseDown
        {
            get;
            set;
        }

        public static Vector2 MousePosition
        {
            get;
            set;
        }

        public static void SetSource(UIElement element)
        {
            _KeyState = new Dictionary<Key, bool>();

            LeftMouseDown = false;
            RightMouseDown = false;
            MousePosition = Vector2.Zero;

            element.KeyDown += OnKeyDown;
            element.KeyUp += OnKeyUp;
            element.MouseLeftButtonDown += OnLeftMouseDown;
            element.MouseLeftButtonUp += OnLeftMouseUp;
            element.MouseRightButtonDown += OnRightMouseDown;
            element.MouseRightButtonUp += OnRightMouseUp;
            element.MouseMove += OnMouseMove;
        }

        public static bool IsKeyDown(Key key)
        {
            bool down = false;

            if (_KeyState.ContainsKey(key))
            {
                down = _KeyState[key];
            }
            return down;
        }

        private static void OnKeyDown(object sender, KeyEventArgs e)
        {
            _KeyState[e.Key] = true;
        }

        private static void OnKeyUp(object sender, KeyEventArgs e)
        {
            _KeyState[e.Key] = false;
        }

        private static void OnLeftMouseDown(object sender, MouseButtonEventArgs e)
        {
            LeftMouseDown = true;
        }

        private static void OnLeftMouseUp(object sender, MouseButtonEventArgs e)
        {
            LeftMouseDown = false;
        }

        private static void OnRightMouseDown(object sender, MouseButtonEventArgs e)
        {
            RightMouseDown = true;
        }

        private static void OnRightMouseUp(object sender, MouseButtonEventArgs e)
        {
            RightMouseDown = false;
        }

        private static void OnMouseMove(object sender, MouseEventArgs e)
        {
            MousePosition = new Vector2((float)e.GetPosition(null).X, (float)e.GetPosition(null).Y);
        }
    }
}
